﻿using System;
using System.Collections.Generic;

namespace FJson
{
    public class LiteDictionary<TKey, TValue>
    {
        public LiteDictionary()
        {
            this.Keys   = new List<TKey>();
            this.Values = new List<TValue>();
        }

        public List<TKey>   Keys;
        public List<TValue> Values;

        public int Count => this.Keys.Count;

        public TValue this[TKey key]
        {
            get
            {
                for (int i = 0; i < this.Keys.Count; i++)
                {
                    if (this.Keys[i].Equals(key))
                    {
                        return this.Values[i];
                    }
                }

                throw new KeyNotFoundException();
            }
            set
            {
                var index = ContainsKeyNum(key);
                if (index == -1)
                {
                    this.Keys.Add(key);
                    this.Values.Add(value);
                }
                else
                {
                    this.Values[index] = value;
                }
            }
        }

        public int ContainsKeyNum(TKey key)
        {
            for (int i = 0; i < this.Keys.Count; i++)
            {
                if (this.Keys[i].Equals(key))
                {
                    return i;
                }
            }

            return -1;
        }

        public bool ContainsKey(TKey key)
        {
            int t = ContainsKeyNum(key);
            return t != -1;
        }

        public void Add(TKey key, TValue value)
        {
            var index = ContainsKeyNum(key);
            if (index == -1)
            {
                this.Keys.Add(key);
                this.Values.Add(value);
            }
            else
            {
                throw new Exception("key重复添加");
            }
        }

        public void Remove(TKey key)
        {
            var index = ContainsKeyNum(key);
            if (index != -1)
            {
                this.Keys.RemoveAt(index);
                this.Values.RemoveAt(index);
            }
        }

        public void RemoveAt(int index)
        {
            this.Keys.RemoveAt(index);
            this.Values.RemoveAt(index);
        }

        public void Clear()
        {
            this.Keys.Clear();
            this.Values.Clear();
        }

        public TValue GetValue(TKey key)
        {
            for (int i = 0; i < this.Keys.Count; i++)
            {
                if (this.Keys[i].Equals(key))
                {
                    return this.Values[i];
                }
            }

            return default;
        }

        public Dictionary<TKey, TValue> ToDictionary()
        {
            Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
            for (int i = 0; i < Count; i++)
            {
                dictionary.Add(this.Keys[i], this.Values[i]);
            }
            return dictionary;
        }

        public void FormDictionary(Dictionary<TKey, TValue> dictionary)
        {
            this.Keys   = new List<TKey>(dictionary.Keys);
            this.Values = new List<TValue>(dictionary.Values);
        }
    }
}